
import config from "./config"; 

  // 请求发送的次数
  let  ajaxnum = 0
  export  const  request = (url,data={},method='GET')=>{
    ajaxnum++
    wx.showLoading({title: '加载中...'})
    return new Promise((resolve,reject)=>{ 
      wx.request({
        url: config.hosts + url,
        data,
        method,
        header:{
          cookie: wx.getStorageSync('cookie')?wx.getStorageSync('cookie').find(item => item.indexOf('MUSIC_U') !== -1):''
        },
        success:(res)=>{ 
          if(data.isLogin){ 
              wx.setStorage({
                key: 'cookie',
                data: res.cookies
              })
          }
          resolve(res.data);
        },
        fail: (err) => { 
          reject(err); // reject修改promise的状态为失败状态 rejected
        },
        complete:()=>{
          ajaxnum--
          if(ajaxnum==0){
            wx.hideLoading()
          }
        }
      })
    })
  }